const vscode = require('vscode');

const PLUGIN_KEY = 'hyj.tool.collect'

/**
 * 插件被激活时触发，所有代码总入口
 * @param {*} context 插件上下文
 */
exports.activate = function (context) {
  console.log('激活工具收藏插件（vscode-tool-collect-plugin）');
  // 注册命令
  context.subscriptions.push(vscode.commands.registerCommand(PLUGIN_KEY, function () {
    // 获取配置
    let config = vscode.workspace.getConfiguration(PLUGIN_KEY)

    // 按下后执行事件
    let options = [
      {
        label: '转换: 转驼峰',
        description: "alt+1 aaaBbbCcc",
        command: "extension.changeCase.camel"
      },
      {
        label: '转换: 转下划线',
        description: "alt+2 aaa_bbb_ccc",
        command: "extension.changeCase.snake"
      },
      {
        label: '转换: 首字母大写驼峰',
        description: "alt+3 AaaBbbCcc",
        command: "extension.changeCase.pascal"
      },
      {
        label: '转换: 短横',
        description: "alt+4 aaa-bbb-ccc",
        command: "extension.changeCase.param"
      },
      {
        label: '转换: 路径',
        description: "alt+5 aaa/bbb/ccc",
        command: "extension.changeCase.path"
      },
      {
        label: '转换: 打开command',
        description: "ctrl+shift+r",
        command: "extension.changeCase.commands"
      },
      {
        label: '复制: 向下复制一行',
        description: "ctrl+shift+c",
        command: "editor.action.copyLinesDownAction"
      },

      // {
      //   label: 'fuck2',
      //   detail: "detaildetaildetail",
      //   description: "descriptiondescriptiondescription",
      // }
    ];
    if (config.includes?.length){
      let errors = [];
      config.includes.forEach(item => {
        if (!item) return;
        if (!item.label){
          errors.push(item);
          return;
        }
        options.push(item);
      });
      if (errors.length){
        vscode.window.showInformationMessage('【工具收藏】选项没有配置label字段\n' + JSON.stringify(errors));
      }
    }
    // 排除配置
    if (config.excludes?.length){
      options = options.filter(item => !config.excludes.includes(item.label));
    }


    vscode.window.showQuickPick(options, {
      placeHolder: "输入搜索命令",
      title: "工具收藏",
      ignoreFocusOut: true,
    }).then(res=>{
      if (!res){
        return;
      }
      console.log(res);
      // 执行命令
      if (res.command){
        vscode.commands.executeCommand(res.command);
      }

    });
    // vscode.window.showInformationMessage('Hello World!');
  }));
};

/**
 * 插件被释放时触发
 */
exports.deactivate = function () {
  console.log('您的扩展“vscode-plugin-demo”已被释放！')
};
